<?php

namespace app\admin\controller;


use app\common\exception\ApiException;
use think\Cache;
use think\Controller;

class BaseController extends Controller {
    public $user_id;
    public $page = 1;
    public $size = 10;
    public $from = 0;

    protected $beforeActionList = [
        'checkToken' => ['except' => 'check']
    ];

    protected function checkToken() {
        $token = request()->header('token');
        if ($token === 'undefined') {
            throw new ApiException('token不存在，请登录', 200, 10);
        }
        if (Cache::get($token)) {
            return true;
        } else {
            throw new ApiException('token不存在，请登录', 200, 10);
        }
    }

    /*获取分页page size 内容*/
    public function getPageAndSize($data) {
        $this->page = !empty($data['page']) ? $data['page'] : 1;
        $this->size = !empty($data['size']) ? $data['size'] : config('paginate.list_rows');
        $this->from = ($this->page - 1) * $this->size;
    }
}